﻿#include <iostream>
#include <sstream>
#include <random>
#include <memory>
#include <string>
#include "AutoPtr.h"

//using namespace std;

class B;
class A
{
	public :
	//std::shared_ptr<B> pb_;
	std::weak_ptr<B> pb_;
	~A()
	{
		std::cout << "A delete\n";
	}
}
;
class B
{
	public :
	std::shared_ptr<A> pa_;
	
	
	~B()
	{
		std::cout << "B delete\n";
	}
}
;
void fun()
{
	std::shared_ptr<B> pb(new B());
	std::shared_ptr<A> pa(new A());
	pb->pa_ = pa;
	pa->pb_ = pb;
	std::cout << pb.use_count() << std::endl;
	std::cout << pa.use_count() << std::endl;
} 

int main()
{
	fun();
	return 0;
}


//int main()
//{
//    // 创建两个 shared_ptr 对象
//    std::shared_ptr<int> ptr1 = std::make_shared<int>(42);
//    std::shared_ptr<int> ptr2 = std::make_shared<int>(100);
//    std::shared_ptr<int> ptr3 = ptr1;
//
//    std::cout << "ptr1: " << *ptr1 << ", use_count: " << ptr1.use_count() << std::endl;
//    std::cout << "ptr2: " << *ptr2 << ", use_count: " << ptr2.use_count() << std::endl;
//
//    // 使用 swap 交换资源
//    ptr1.swap(ptr2);
//
//    std::cout << "ptr1: " << *ptr1 << ", use_count: " << ptr1.use_count() << std::endl;
//    std::cout << "ptr2: " << *ptr2 << ", use_count: " << ptr2.use_count() << std::endl;
//
//
//
//    //std::shared_ptr<int> p1(new int(10));
//    //std::shared_ptr<int> p2 = p1;
//
//    //std::cout << "p1 is unique: " << std::boolalpha << p1.unique() << std::endl; // 输出：p1 is unique: false
//    //std::cout << "p2 is unique: " << std::boolalpha << p2.unique() << std::endl; // 输出：p2 is unique: false
//
//    //std::shared_ptr<int> p3(new int(20));
//    //std::cout << "p3 is unique: " << std::boolalpha << p3.unique() << std::endl; // 输出：p3 is unique: true
//
//    //p2.reset();
//    //std::cout << "p1 is unique: " << std::boolalpha << p1.unique() << std::endl; // 输出：p1 is unique: true
//    //std::cout << "p2 is unique: " << std::boolalpha << p2.unique() << std::endl; // 输出：p2 is unique: false
//
//
//
//
////    std::shared_ptr<std::string> sp1;
////
////    std::shared_ptr<std::string> sp2(new std::string("test"));
////
////    std::cout << "sp1	use_count() = " << sp1.use_count() << std::endl;    // 未发生拷贝或复制 0
////    std::cout << "sp2	use_count() = " << sp2.use_count() << std::endl << std::endl;  // 拷贝1次 +1
////
////    // 共享
////    sp1 = sp2;  // 赋值一次，sp2 +1，sp1内部变为sp2，所以结果应该与sp2一致
////
////    std::cout << "sp1	use_count() = " << sp1.use_count() << std::endl;  // 打印 2
////    std::cout << "sp2	use_count() = " << sp2.use_count() << std::endl << std::endl;  // 打印 2
////
////    std::shared_ptr<std::string> sp3(sp1);  // sp3拷贝sp1； sp1 次数+1，sp3与sp1 一致
////    std::cout << "sp1	use_count() = " << sp1.use_count() << std::endl;  // 打印 3
////    std::cout << "sp2	use_count() = " << sp2.use_count() << std::endl;  // 打印 3
////    std::cout << "sp2	use_count() = " << sp3.use_count() << std::endl << std::endl;  // 打印 3
////
////                                                                                       
////// reset使用
////    sp1.reset();
////    std::cout << "sp1	use_count() = " << sp1.use_count() << " " << sp1 << std::endl;
////    std::cout << "sp2	use_count() = " << sp2.use_count() << " " << sp2 << std::endl;
////    std::cout << "sp3	use_count() = " << sp3.use_count() << " " << sp3 << std::endl << std::endl;
//
//
//
//    return 0;
//}
